<template>
  <div class="count">
    <h1>当前求和为:{{ sum }}</h1>
    <h3>当前求和放大10倍为:{{ bigSum }}</h3>
    <h3>我在{{ school }},学习{{ subject }}</h3>
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment(n)">+</button>
    <button @click="decrement(n)">-</button>
    <button @click="incrementOdd(n)">当前求和为奇数再加</button>
    <button @click="incrementWait(n)">等一等再加</button>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions, mapMutations } from "vuex";
export default {
  name: "count-com",
  data() {
    return {
      n: 1, // 用户选择的数字
    };
  },
  methods: {
    // 程序员亲自写方法
    // increment(){
    //   this.$store.commit('AddHandler', this.n)
    // },
    // decrement() {
    //   this.$store.commit('SubHandler', this.n)
    // },
    // incrementOdd() {
    //   this.$store.commit('OddHandler', this.n)
    // },

    // 借助 mapMutations 生成对应的方法,方法中会调用 commit 去联系 mutations (对象写法)
    ...mapMutations({
      increment: "AddHandler",
      decrement: "SubHandler",
      incrementOdd: "OddHandler",
    }),

    // 借助 mapMutations 生成对应的方法,方法中会调用 commit 去联系 mutations (数组写法)
    // ...mapMutations(['increment','decrement','incrementOdd']),

    /* ************************************************* */

    // 程序员亲自写方法
    // incrementWait() {
    //   this.$store.dispatch('waitHandler', this.n)
    // }

    // 借助 mapActions 生成对应的方法,方法中会调用 dispatch 去联系 actions(对象写法)
    ...mapActions({ incrementWait: "wait-Handler" }),

    //借助 mapActions 生成对应的方法,方法中会调用 dispatch 去联系 actions (数组写法)
    // ...mapActions(['incrementWait']),
  },
  computed: {
    ...mapState(["sum", "school", "subject"]),
    ...mapGetters(["bigSum"]),
  },
};
</script>

<style lang="sass">
$margin-left: 10px

button
  margin: $margin-left
</style>
